Go back to the Preprocessing page. This link might be useful to keep track of the files created during the preprocessing.

Let us set some global options for all code chunks in this document.

knitr::opts_chunk$set(
  message = FALSE,    # Disable messages printed by R code chunks
  warning = FALSE,    # Disable warnings printed by R code chunks
  echo = TRUE,        # Show R code within code chunks in output
  include = TRUE,     # Include both R code and its results in output
  eval = TRUE,       # Evaluate R code chunks
  cache = FALSE,       # Enable caching of R code chunks for faster rendering
  fig.align = "center",
  out.width = "100%",
  retina = 2,
  error = TRUE,
  collapse = FALSE
)
rm(list = ls())
set.seed(1982)

1 Import libraries

# Install R-INLA package
# install.packages("INLA",repos = c(getOption("repos"),INLA ="https://inla.r-inla-download.org/R/testing"), dep = TRUE)
# Update R-INLA package
# inla.upgrade(testing = TRUE)
# Install inlabru package
# remotes::install_github("inlabru-org/inlabru", ref = "devel")
# Install rSPDE package
# remotes::install_github("davidbolin/rspde", ref = "devel")
# Install MetricGraph package
# remotes::install_github("davidbolin/metricgraph", ref = "devel")

library(INLA)
library(inlabru)
library(rSPDE)
library(MetricGraph)

library(plotly)
library(dplyr)
library(tidyr)
library(sf)

library(here) # here() starts from the home directory
library(rmarkdown)

rm(list = ls()) # Clear the workspace
set.seed(1982) # Set seed for reproducibility

2 Load the data

# Load the january dataset
load(here("data_files/january.RData"))

3 Function to remove consecutive zeros from the speed variable

# Function to remove consecutive zeros
remove_consecutive_zeros <- function(vec) {
  # Initialize a result vector
  result <- numeric(length(vec))
  # Index for the result vector
  index <- 1
  # Flag to track if the first zero has been encountered
  first_zero <- FALSE
  # Loop through the original vector
  for (i in 1:length(vec)) {
    # If current value is not zero or previous value is not zero, or it's the first zero, add it to result
    if (vec[i] != 0 || (i > 1 && vec[i - 1] != 0) || i == 1) {
      result[index] <- vec[i]
      index <- index + 1
      # Reset first_zero flag if it's the first zero
      if (vec[i] == 0 && !first_zero) {
        first_zero <- TRUE
      }
    } else {
      # Replace consecutive zeros with NA after the first zero
      result[index] <- NA
      index <- index + 1
    }
  }
  # Trim the result vector to remove unused entries
  result <- result[1:(index - 1)]
  return(result)
}

4 Remove consecutive zeros from the speed variable

# Choose the days for the analysis
days <- c(7,14,21,28) # every Thursday of January 2021

# Filter by days and hour of interest
aux <- january %>%
  filter(day %in% days, hour %in% c(13)) %>% # Keep observations between 13:00 and 14:00
  dplyr::select(-PDT, -hour) # Remove PDT and hour variables

# Get the unique buses ID
buses_ID <- unique(aux$ID)

# Remove more than one consecutive zeros from the speed variable and store the data in a new data frame df
df <- aux %>% 
  filter(ID == buses_ID[1], day == days[1]) %>% # Get the first bus in the first day
  arrange(datetime) %>% # Arrange by datetime so that we can remove consecutive zeros
  mutate(speed = remove_consecutive_zeros(speed)) %>% # Remove consecutive zeros
  drop_na(speed) # Drop NAs, if any

for (i in 1:length(buses_ID)) { # Loop through the buses
  for (j in 1:length(days)) { # Loop through the days
    if (i == 1 && j == 1) { # Skip the first bus in the first day, as it has been processed already
      next 
    }
    check <- aux %>% filter(ID == buses_ID[i], day == days[j]) # Get the data for the bus and day
    if(nrow(check) > 0){ # If there is data, process as before and append to df
      tmp <- check %>% 
        arrange(datetime) %>%
        mutate(speed = remove_consecutive_zeros(speed)) %>%
        drop_na(speed)
      df <- rbind(tmp, df)
    }
  }
}

# Change the days from days to 1,2,3, and 4 to make it easier to work with
newdays <- 1:4
df$day <- newdays[match(df$day, days)]

# Save the data corresponding to 7,14,21, and 28 January 2021 during 13:00-14:00 with no consecutive zeros in the speed variable
save(df, file = here("data_files/day7142128hour13noconsecutivezeroes.RData"))

5 Check the data

# Choose a bus ID
IDnumber <- 6697

# Get the data with all zeros for the chosen bus ID 
bus <- january %>% 
  filter(day == 7, ID == IDnumber, hour == 13) %>% 
  arrange(datetime) %>%
  dplyr::select(datetime, speed)

# Plot the speed records for the chosen bus ID
TSstudio::ts_plot(bus,
                  line.mode = "lines+markers",
                  title = paste("Speed records, all zeros, 7 January, 1-2pm, bus ID: ", IDnumber, sep = ""),
                  Xtitle = "Time",
                  Ytitle = "Speed")
# Get the data with no consecutive zeros for the chosen bus ID
bus <- january %>% 
  filter(day == 7, ID == IDnumber, hour == 13) %>% 
  arrange(datetime) %>%
  mutate(speed = remove_consecutive_zeros(speed)) %>%
  drop_na(speed) %>%
  dplyr::select(datetime, speed)

# Plot the speed records for the chosen bus ID
TSstudio::ts_plot(bus,
                  line.mode = "lines+markers",
                  title = paste("Speed records, no consecutive zeros, 7 January, 1-2pm, bus ID: ", IDnumber, sep = ""),
                  Xtitle = "Time",
                  Ytitle = "Speed")
# Get the data with no consecutive zeros for the chosen bus ID (now from the new data frame df to check if it is the same)
bus <- df %>% 
  filter(day == 1, ID == IDnumber) %>%  # Note that day 1 in df corresponds to 7 in january
  arrange(datetime) %>%
  dplyr::select(datetime, speed)

# Plot the speed records for the chosen bus ID
TSstudio::ts_plot(bus,
                  line.mode = "lines+markers",
                  title = paste("Speed records, no consecutive zeros, 7 January, 1-2pm, bus ID: ", IDnumber, sep = ""),
                  Xtitle = "Time",
                  Ytitle = "Speed")